﻿/*
 * Last review by   : Yaser
 * Last review date : JUL-25-09 
 * TODO: implement functions
 * TODO: use tracing util
 */
using System;
using ERP.Framework.Components.Utilities.TracingHelper;
using ERP.Business.DataTypes.Users;
using ERP.Framework.Components.DatabaseHelper.Providers;
using System.Data;
using ERP.Framework.Components.DatabaseHelper;
using ERP.Framework.Components.SerializationHelper;
using System.Xml;
using System.Text;

namespace ERP.Business.Users
{
    /// <summary>
    /// User's related operations
    /// </summary>
    public sealed class UsersManager
    {
        /*
        private const string TRACING_CATEGORY = "ERP.Business.Users.UsersManager";

        private const string SP_GET_USER_INFO_BY_USERNAME = "SP_USERS_GET_INFO_BY_USERNAME";
        private const string SP_UPD_INS_USER_INFO = "SP_USERS_INSERT_UPDATE";
        private const string PARAM_USERNAME = "username";
        private const string PARAM_PASSWORD = "password";
        private const string PARAM_USER_ID = "id";
        private const string PARAM_EMPLOYEE_ID = "employeeId";
        private const string PARAM_USER_STATUS = "status";

        /// <summary>
        /// Create a new user.
        /// </summary>
        /// <remarks>A several exceptions could be thrown depends on the workflow result.</remarks>
        /// <param name="user">new user info</param>
        /// <param name="employeeID">related employee id</param>
        /// <returns>result: true if creation completed successfully</returns>
        public static bool CreateUser(ERP.Business.DataTypes.Users.User user, string employeeID)
        {
            return CreateUser(user.Username, user.Password, employeeID);
        }

        /// <summary>
        /// Create a new user.
        /// </summary>
        /// <remarks>A several exceptions could be thrown depends on the workflow result.</remarks>
        /// <param name="user">new user info</param>
        /// <param name="employee">realted employee</param>
        /// <returns>result: true if creation completed successfully</returns>
        public static bool CreateUser(ERP.Business.DataTypes.Users.User user, ERP.Business.DataTypes.Employee.Employee employee)
        {
            return CreateUser(user.Username, user.Password, employee.ID);
        }

        /// <summary>
        /// Create a new user.
        /// </summary>
        /// <remarks>A several exceptions could be thrown depends on the workflow result.</remarks>
        /// <param name="username">username</param>
        /// <param name="password">password</param>
        /// <param name="employeeId">related employee id</param>
        /// <returns>result: true if creation completed successfully</returns>
        public static bool CreateUser(string username, string password, string employeeId)
        {
            throw new NotImplementedException();
        }

        public static User GetUser(string username)
        {
            TraceUtil.Write(TRACING_CATEGORY, "GetUser : Start", DateTime.Now);
            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);
            ParametersCollection param = new ParametersCollection();
            param.AddParameter(PARAM_USERNAME, username, ParameterType.String, ERP.Framework.Components.DatabaseHelper.ParameterDirection.InParameter);
            DataTable table = con.Read(ERP.Framework.Components.DatabaseHelper.CommandType.StoredProcedure, SP_GET_USER_INFO_BY_USERNAME, param);

            User user = null;
            if (table != null && table.Rows.Count > 0)
            {
                table.TableName = "User";
                StringBuilder sb = new StringBuilder();
                using (XmlWriter x = XmlWriter.Create(sb))
                {
                    table.WriteXml(x);
                }
                user = (User)XmlSerializingManager.DeserializeObject<User>(sb.ToString());
            }

            TraceUtil.Write(TRACING_CATEGORY, "GetUser : End", DateTime.Now);

            return user;
        }

        public static bool UpdateUserInfo(User updatedUserInfo)
        {
            TraceUtil.Write(TRACING_CATEGORY, "UpdateUserInfo : Start", DateTime.Now);

            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);
            ParametersCollection param = new ParametersCollection();
            param.AddParameter(PARAM_USERNAME, updatedUserInfo.Username, ParameterType.String, ERP.Framework.Components.DatabaseHelper.ParameterDirection.InParameter);
            param.AddParameter(PARAM_PASSWORD, updatedUserInfo.Password, ParameterType.String, ERP.Framework.Components.DatabaseHelper.ParameterDirection.InParameter);
            param.AddParameter(PARAM_USER_ID, updatedUserInfo.ID, ParameterType.String, ERP.Framework.Components.DatabaseHelper.ParameterDirection.InParameter);
            param.AddParameter(PARAM_USER_STATUS, ((int)updatedUserInfo.Status), ParameterType.Int, ERP.Framework.Components.DatabaseHelper.ParameterDirection.InParameter);
            if (updatedUserInfo.EmployeeID.Equals(string.Empty))
                param.AddParameter(PARAM_EMPLOYEE_ID, DBNull.Value, ParameterType.String, ERP.Framework.Components.DatabaseHelper.ParameterDirection.InParameter);
            else
                param.AddParameter(PARAM_EMPLOYEE_ID, updatedUserInfo.EmployeeID, ParameterType.String, ERP.Framework.Components.DatabaseHelper.ParameterDirection.InParameter);
            
            int res = con.Save(ERP.Framework.Components.DatabaseHelper.CommandType.StoredProcedure, SP_UPD_INS_USER_INFO, param);

            TraceUtil.Write(TRACING_CATEGORY, "UpdateUserInfo : End", DateTime.Now);
            return res > 0;
        }*/
    }
}